using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
#if UNITY_ANDROID
using UnityEngine.Android;
#endif

namespace Gj
{
    public enum PermissionDefine
    {
        _,
        ExternalStorageWrite,
    }
    public enum PermissionResult
    {
        Ok,
        Refuse,
        Try,
        Again,
    }
    public class AndroidPermission: FocusSystem, PauseSystem
    {
        private static Dictionary<PermissionDefine, string> DefineMap = new Dictionary<PermissionDefine, string>();
        private static Dictionary<string, PermissionDefine> PermissionMap = new Dictionary<string, PermissionDefine>();
        private static Dictionary<PermissionDefine, PermissionResult> ResultMap = new Dictionary<PermissionDefine, PermissionResult>();
        private static Dictionary<PermissionDefine, Action<PermissionResult>> CallbackMap = new Dictionary<PermissionDefine, Action<PermissionResult>>();

        private static Stack<PermissionDefine> stack = new Stack<PermissionDefine>();
        public static AndroidPermission single;
        public static PermissionDefine currentDefine;
        public static bool waitFocus = false;
        static AndroidPermission()
        {
#if UNITY_ANDROID
            DefineMap.Add(PermissionDefine.ExternalStorageWrite, Permission.ExternalStorageWrite);
            PermissionMap.Add(Permission.ExternalStorageWrite, PermissionDefine.ExternalStorageWrite);
#endif
            single = new AndroidPermission();
        }

        public static PermissionDefine ToDefine(string permission)
        {
            var define = PermissionMap[permission];
            return define;
        }

        public static string ToPermission(PermissionDefine define)
        {
            var permission = DefineMap[define];
            return permission;
        }

        // Has在进入的时候都判断
        public static PermissionResult Has(PermissionDefine define, bool inline = false)
        {
            PermissionResult result = PermissionResult.Ok;
#if UNITY_ANDROID
            var permission = DefineMap[define];
            if (ResultMap.ContainsKey(define))
            {
                result = ResultMap[define];

                switch (result)
                {
                    case PermissionResult.Try:
                        if (!Permission.HasUserAuthorizedPermission(permission))
                        {
                            result = PermissionResult.Again;
                        }
                        else
                        {
                            result = PermissionResult.Ok;
                        }
                        if (!inline) ResultMap[define] = result;
                        break;
                    case PermissionResult.Again:
                        if (!Permission.HasUserAuthorizedPermission(permission))
                        {
                            result = PermissionResult.Refuse;
                        }
                        else
                        {
                            result = PermissionResult.Ok;
                        }
                        if (!inline) ResultMap[define] = result;
                        break;
                }
            }
            else
            {
                // 第一次先判断是否授权，如果没有调用授权
                if (!Permission.HasUserAuthorizedPermission(permission))
                {
                    result = PermissionResult.Try;
                }
                else
                {
                    result = PermissionResult.Ok;
                }
                if (!inline) ResultMap.Add(define, result);
            }
#endif
            return result;
        }

        public static void BindCallback(PermissionDefine define, Action<PermissionResult> callback)
        {
            Debug.LogError("permission callback:"+define);
            if (CallbackMap.ContainsKey(define))
            {
                CallbackMap[define] = callback;
            }
            else
            {
                CallbackMap.Add(define, callback);
            }
            if (currentDefine == 0)
            {
                if (!waitFocus)
                {
                    waitFocus = true;
                    Game.single.RegisterPause(single);
                }
                Request(define);
            } 
            else
            {
                stack.Push(define);
            }
        }

        public static void Next()
        {
            if (stack.Count == 0)
            {
                currentDefine = 0;
                waitFocus = false;
                Game.single.UnRegisterPause(single);
            }
            else
            {
                var define = stack.Pop();
                Request(define);
            }
        }

        // Has返回Try可以给用户弹框提示，确认后使用Request
        public static void Request(PermissionDefine define)
        {
            Debug.LogError("permission request:"+define);
#if UNITY_ANDROID
            // 主动提示授权，确认后再次授权
            var permission = DefineMap[define];
            if (ResultMap.ContainsKey(define))
            {
                var result = ResultMap[define];
                if (result == PermissionResult.Try)
                {
                    if (!Permission.HasUserAuthorizedPermission(permission))
                    {
                        currentDefine = define;
                        Permission.RequestUserPermission(permission);
                    }
                }
                else if (result == PermissionResult.Again)
                {
                    if (!Permission.HasUserAuthorizedPermission(permission))
                    {
                        currentDefine = define;
                        Permission.RequestUserPermission(permission);
                    }
                }
            }
            else
            {
                // 请先Has判断权限，如果try，绑定回调
                // 如果again，执行request
            }
#endif
        }

        public void OnFocus()
        {
            Debug.LogError("permission onFocus:"+currentDefine);
            if (currentDefine == PermissionDefine._) return;
            var result = Has(currentDefine, true);
            if (CallbackMap.ContainsKey(currentDefine))
            {
                CallbackMap[currentDefine](result);
            }
            if (result == PermissionResult.Ok || result == PermissionResult.Refuse)
            {
                // 自动请求下一个权限
                Next();
            }
        }
        public void OnPause()
        {
        }
        public void OnResume()
        {
            Debug.LogError("permission onResume:"+currentDefine);
            if (currentDefine == PermissionDefine._) return;
            var result = Has(currentDefine, true);
            if (CallbackMap.ContainsKey(currentDefine))
            {
                CallbackMap[currentDefine](result);
            }
            if (result == PermissionResult.Ok || result == PermissionResult.Refuse)
            {
                // 自动请求下一个权限
                Next();
            }
        }

    }
}